package prototyping.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.UnknownHostException;

public class ServerListener extends Thread {

	private ObjectInputStream sInput;
	private AbstractClient aClient;
	
	public ServerListener(ObjectInputStream sInput, AbstractClient aClient) {
		this.sInput = sInput;
		this.aClient = aClient;
	}
	
	public void run() {
		AbstractMessage amsg = null;
		/* TODO Subclass Abstactmessage with ErrorMessage and Servicemessage for custom
		 * messages not related to chat or help.
		 */
		while(true) {
			try {
				amsg = (AbstractMessage) sInput.readObject();
			}
			catch(IOException e) {
				try {
					amsg = new ChatMessage("Server has closed the connection: " + e);
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				}
			} catch (ClassNotFoundException e) {
				try {
					amsg = new ChatMessage("Problem getting message form server: " + e);
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				}
			}
			finally{
				aClient.display(amsg);
			}
			
		}
	}
	
}
